package com.gemalto.Kryptik.Gui;

import javax.swing.*;
import java.awt.Color;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.JWindow;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.JLabel;
import java.awt.event.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.*;


public class MessageArea extends JLabel implements MouseListener, MessageListener {

	private String defaultText, text;
	private Timer timer;
	private JWindow w;
	private boolean beep_on_error, beep_on_warning;
	private	ImageIcon errorIcon = Util.getIcon("error.gif");
	private	ImageIcon warningIcon = Util.getIcon("NetworkHand.gif");
	private IniFiles ini;
	
	public MessageArea(String s){
		super();

		ini = Util.getIni();
		
		defaultText=s;
//      		setDisabledTextColor(new java.awt.Color (102, 102, 153));
		setBorder(new javax.swing.border.EtchedBorder());
//		setEditable(false);

          	setForeground((java.awt.Color) javax.swing.UIManager.getDefaults ().get ("CheckBoxMenuItem.acceleratorForeground"));
          	setFont(new java.awt.Font ("Dialog", 0, 11));

          	setMaximumSize(new java.awt.Dimension(Short.MAX_VALUE, 20));
          	setPreferredSize(new java.awt.Dimension(200, 20));
          	setMinimumSize(new java.awt.Dimension(100, 20));
          	addMouseListener(this);

		Messenger.addListener(this);


/*			new java.awt.event.ActionListener() {
              		public void actionPerformed(java.awt.event.ActionEvent evt) {
                  		System.out.println("EVENT");
              		}
          	});
  */
	 	timer = new Timer(ini.getIntValue("MESSAGE_AREA","TIMEOUT"), new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				setForeground((java.awt.Color) javax.swing.UIManager.getDefaults ().get ("CheckBoxMenuItem.acceleratorForeground"));
				setIcon(null);
				setText(defaultText);
				timer.stop();
	            }});
		setText(defaultText);
		text="";
		beep_on_warning=ini.getBoolValue("MESSAGE_AREA","BEEP_ON_WARNING");
		beep_on_error=ini.getBoolValue("MESSAGE_AREA","BEEP_ON_ERROR");
	}




	public void mouseEntered(MouseEvent e){
	}

	public void mouseExited(MouseEvent e){
	}

	public void mouseClicked(MouseEvent e){
	}

	public void mousePressed(MouseEvent e){
		JLabel l = new JLabel(getHtmlized(text));
		l.setBorder(BorderFactory.createLineBorder(Color.black));
		w = new JWindow();
		w.getContentPane().add(l);
		w.pack();
		w.setLocation(5,5);
		w.show();
	}

	public void mouseReleased(MouseEvent e){
		w.dispose();
		w=null;
	}



	private String getHtmlized(String text){
		int sIndex=0, eIndex=0;
		boolean isHtml=false;
		
		StringBuffer sb=new StringBuffer();
	
		eIndex=text.indexOf('\n');
		
		if (eIndex==-1) sb.append(text);
		else { 
			isHtml=true;
			sb.append("<html>");
			sb.append("<font color=\"green\">Last warning:</font><p>");
		 }

		while (eIndex > 0){
			sb.append(text.substring(sIndex,eIndex));
			sb.append("<p>");
			sIndex=eIndex;
			eIndex=text.indexOf('\n',sIndex+1);
		}

		if ( isHtml ) {
			sb.append(text.substring(sIndex));
			sb.append("</html>");
		}

		return sb.toString();
	}



	private String getFirstLine(String text){
		if (text==null) return null;
		int index=text.indexOf('\n');
		if (index>0) text=text.substring(0,index);
		return text;
	}


	public void error(String text){
		this.text=text;
		setForeground(Color.red);
		setIcon(errorIcon);
		setText(getFirstLine(text));
//		setToolTipText(getHtmlized(text));
		 
		if (beep_on_error) Toolkit.getDefaultToolkit().beep();
		timer.start();
	}


	public void warning(String text){
		this.text=text;
		setForeground(Color.blue);
		setIcon(warningIcon);
		setText(getFirstLine(text));
//		setToolTipText(getHtmlized(text));
		 
		if (beep_on_warning) Toolkit.getDefaultToolkit().beep();
		timer.start();
	}


}
